package offer.diff;

import java.util.Arrays;

/**
 * @author DengYuan2
 * @create 2021-03-19 18:49
 */
public class n_48 {
    public static void main(String[] args) {
        n_48 n = new n_48();
        int res = n.longestSubStr2("araebcamcfrdgh");
        System.out.println(res);
    }

    public int longestSubStr(String str) {
        int[] check = new int[26];
        Arrays.fill(check, -1);
        int max = 0;
        int sum = 0;
        for (int i = 0; i < str.length(); i++) {
            int x = str.charAt(i) - 'a';
            if (check[x] == -1 || i-check[x]>max) {
                max += 1;
            } else {
                max = i - check[x];
            }
            check[x]=i;
            sum = Math.max(sum, max);
        }
        return sum;
    }


    public int longestSubStr2(String str){
        char[] array = str.toCharArray();
        int[] index = new int[26];
        Arrays.fill(index,-1);
        int max=0;
        int start=0;
        for (int i = 0; i < array.length; i++) {
            int ix=array[i]-'a';
//                start=i;
                if (index[ix]>start){

                    start=index[ix]+1;
                }
            index[ix]=i;
            max=Math.max(max,i-start+1);
        }
        return max;

    }
}
